const app = getApp()
Page({
    data: {
        page: 1,
        datamsg: false,
        moment: false,
        momentList: [],
        itemList: [],
        arr: []
    },
    onLoad: function(options) {
        var _this = this;
        _this.setData({
            tokenID: app.data.tokenID
        })
        _this.onReachBottom();
    },
    onPullDownRefresh: function() {
        var _this = this;
        this.setData({
            page: 1
        })
        wx.showNavigationBarLoading();
        _this.gtemomentList("down")
    },
    onReachBottom: function() {
        var that = this;
        wx.showLoading({
            title: '玩命加载中',
        })
        that.gtemomentList("up")
    },
    gtemomentList: function(type) {
        var that = this;
        that.setData({
            tokenID: app.data.tokenID
        })
        wx.request({
            url: app.data.api + 'growth/getmygrowth',
            data: {
                "page": that.data.page,
                "token": that.data.tokenID
            },
            method: "POST",
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
                if (res.data.status) {
                    if (type == 'up') {
                        if (that.data.page > 2) {
                            if (res.data.data.list.length == 0) {
                                that.setData({
                                    datamsg: true
                                })
                            }
                        }
                        var arr = that.data.arr;
                        var itemList = that.data.itemList;
                        for (var i = 0; i < res.data.data.list.length; i++) {
                            arr.push(res.data.data.list[i].year)
                            itemList.push(res.data.data.list[i])
                        }
                        that.setData({
                            arr: arr,
                            itemList: itemList
                        })
                        var yearsarr = [];
                        var momentList = that.data.momentList;
                        var jsonItem = []
                        for (var i = 0; i < arr.length - 1; i++) {
                            var json = {}
                            if (yearsarr.indexOf(arr[i]) == -1) {
                                yearsarr.push(arr[i]);
                                json.years = arr[i]
                                json.yearslits = new Array
                                jsonItem.push(json)
                            }
                        }
                        for (var i = 0; i < jsonItem.length; i++) {
                            for (var j = 0; j < itemList.length; j++) {
                                if (jsonItem[i].years == itemList[j].year) {
                                    jsonItem[i].yearslits.push(itemList[j])
                                }
                            }
                        }
                        that.setData({
                            page: that.data.page + 1,
                            momentList: jsonItem
                        })
                    } else {
                        if (res.data.data.list.length == 0) {
                            that.setData({
                                moment: true
                            })
                        }
                        var arr = [];
                        var itemList = [];
                        for (var i = 0; i < res.data.data.list.length; i++) {
                            arr.push(res.data.data.list[i].year)
                            itemList.push(res.data.data.list[i])
                        }
                        that.setData({
                            arr: arr,
                            itemList: itemList
                        })
                        var yearsarr = [];
                        var momentList = [];
                        var jsonItem = []
                        for (var i = 0; i < arr.length - 1; i++) {
                            var json = {}
                            if (yearsarr.indexOf(arr[i]) == -1) {
                                yearsarr.push(arr[i]);
                                json.years = arr[i]
                                json.yearslits = new Array
                                jsonItem.push(json)
                            }
                        }
                        for (var i = 0; i < jsonItem.length; i++) {
                            for (var j = 0; j < itemList.length; j++) {
                                if (jsonItem[i].years == itemList[j].year) {
                                    jsonItem[i].yearslits.push(itemList[j])
                                }
                            }
                        }
                        that.setData({
                            page: that.data.page + 1,
                            momentList: jsonItem
                        })
                        wx.stopPullDownRefresh();
                    }
                    wx.hideLoading();
                    wx.hideNavigationBarLoading();
                } else {
                    wx.hideLoading();
                    wx.showModal({
                        title: '提示',
                        content: res.data.msg,
                        showCancel: false,
                        confirmColor: '#04A9F4',
                        success: function(res) {
                            if (res.confirm) {
                                wx.redirectTo({
                                    url: '../login/login'
                                })
                            } else if (res.cancel) {
                                wx.redirectTo({
                                    url: '../login/login'
                                })
                            }
                        }
                    });
                }
            }
        })
    }
})